package com.zut.hospital.service.examination;

import com.alibaba.fastjson.JSON;
import com.zut.hospital.dao.examination.DepartMapper;
import com.zut.hospital.dao.examination.ResultMapper;
import com.zut.hospital.dao.examination.Stu_Pro_RelationMapper;
import com.zut.hospital.dao.examination.StudentMapper;
import com.zut.hospital.entity.examination.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @PackageName:com.zut.hospital.service.examination
 * @Date:2019/6/28 18:19
 * @Author: zsy
 */
@Service
public class ResultServiceImpl implements ResultService {

    @Autowired
    ResultMapper resultMapper;
    @Autowired
    Stu_Pro_RelationMapper stuProRelationMapper;
    @Autowired
    StudentMapper studentMapper;
    @Autowired
    DepartMapper departMapper;
    @Autowired
    PhyexamService phyexamService;

    @Override
    public String PhyedDept() {
        ResultExample resultExample = new ResultExample();
        List<Result> results = resultMapper.selectByExample(resultExample);

        Map<String, String> resultMap;
        List<Map<String, String>> resultList = new ArrayList<>();

        Stu_Pro_RelationExample stuProRelationExample;
        StudentExample studentExample;
        DepartExample departExample;
        for (Result result : results) {
            resultMap = new HashMap<>();

            stuProRelationExample = new Stu_Pro_RelationExample();
            stuProRelationExample.createCriteria().andIdEqualTo(result.getPhyRelationId());
            Integer stuId = stuProRelationMapper.selectByExample(stuProRelationExample).get(0).getRelatStuId();

            studentExample = new StudentExample();
            studentExample.createCriteria().andIdEqualTo(stuId);
            Student student = studentMapper.selectByExample(studentExample).get(0);

            departExample = new DepartExample();
            departExample.createCriteria().andIdEqualTo(student.getStuDepartId());

            List<Depart> departs = departMapper.selectByExample(departExample);
            for (Depart depart : departs) {
                resultMap.put("deptId", depart.getId().toString());
                resultMap.put("deptName", depart.getDepName());
                studentExample = new StudentExample();
                studentExample.or().andStuDepartIdEqualTo(depart.getId());
                long count = studentMapper.countByExample(studentExample);
                resultMap.put("stuCount", Long.toString(count));
                resultMap.put("phyed", "已录入结果");
            }
            resultList.add(resultMap);
        }
        phyexamService.listLessRepeat(resultList);
        return JSON.toJSONString(resultList);
    }

    @Override
    public String PhyingDept() {
        return null;
    }

    @Override
    public String PhyStu() {
        return null;
    }

    @Override
    public void UpdateResult(Integer id, String value) {
        ResultExample resultExample = new ResultExample();
        resultExample.or().andIdEqualTo(id);
        Result result = resultMapper.selectByExample(resultExample).get(0);
        result.setPhyResult(value);
        resultMapper.updateByExample(result, resultExample);
    }

    @Override
    public void DeleteResult(Integer id) {
        ResultExample resultExample = new ResultExample();
        resultExample.or().andIdEqualTo(id);
        resultMapper.deleteByExample(resultExample);
    }


    @Override
    public void addStudent(Student student) {
        studentMapper.insertSelective(student);
    }

    @Override
    public String allStudent() {
        StudentExample studentExample = new StudentExample();
        List<Student> students = studentMapper.selectByExample(studentExample);

        Map<String, String> resultMap;
        List<Map<String, String>> resultList = new ArrayList<>();

        DepartExample departExample;

        for (Student student : students) {
            resultMap = new HashMap<>();
            resultMap.put("id", student.getId().toString());
            resultMap.put("num", student.getStuNum());
            resultMap.put("name", student.getStuName());
            String sex;
            if (student.getStuSex() == 1) {
                sex = "男";
            } else {
                sex = "女";
            }
            resultMap.put("sex", sex);
            resultMap.put("age", student.getStuAge().toString());
            resultMap.put("nation", student.getStuNation());
            resultMap.put("contact", student.getStuContactInfo());
            resultMap.put("remark", student.getStuRemark());
            Integer deptId = student.getStuDepartId();
            departExample = new DepartExample();
            departExample.or().andIdEqualTo(deptId);
            String deptName = departMapper.selectByExample(departExample).get(0).getDepName();
            resultMap.put("deptName", deptName);
            resultList.add(resultMap);
        }
        return JSON.toJSONString(resultList);
    }
}


